package cn.shiva.cloud.payment.controller;

import cn.shiva.cloud.payment.entity.Payment;
import cn.shiva.cloud.payment.mapper.PaymentMapper;
import cn.shiva.common.basic.Result;
import cn.shiva.common.utils.CodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author shiva   2021/10/19 22:43
 */
@Slf4j
@RestController
@RequestMapping(value = "/payment")
public class PaymentController {

    @Autowired
    private PaymentMapper paymentMapper;

    /**
     * 新增支付单
     */
    @ResponseBody
    @PostMapping(value = "create")
    public Result<Object> create(@RequestBody Payment payment) {
        int resultId = paymentMapper.insert(payment);
        if (resultId > 0) {
            return Result.builder().code(CodeEnum.SUCCESS.code).message("操作成功").data(resultId).build();
        } else {
            return Result.builder().code(CodeEnum.UNSATISFIED.code).message("操作失败").build();
        }
    }

    /**
     * 根据id查询
     */
    @ResponseBody
    @GetMapping(value = "getById")
    public Result<Payment> getById(Long id) {
        Payment payment = paymentMapper.selectById(id);
        if (payment != null) {
            return Result.<Payment>builder().code(CodeEnum.SUCCESS.code).message("查询成功").data(payment).build();
        } else {
            return Result.<Payment>builder().code(CodeEnum.UNSATISFIED.code).message("未查询到指定记录").build();
        }
    }

}
